<?php
/*
 * 商品
 */
namespace Mobile\Controller;
use Mobile\Model\goodsModel;
use Admin\Model\goodstypeModel;
use Mobile\Model\gvoteModel;
use Mobile\Model\goodsguigeModel;
use Mobile\Model\gradeModel;//用户等级


class GoodsController extends BaseController {
    
    public $goodsModel;
    public $goodstypeModel;
    public $goodsguigeModel;
    
    public $gtypes;
    
    public $gradeModel;
    public $grade_info;
    
    public function __construct() {
        parent::__construct();
        $this->goodsModel = new goodsModel();
        $this->goodstypeModel = new goodstypeModel();
        $this->goodsguigeModel = new goodsguigeModel();
        $this->gradeModel = new gradeModel();
        
        $gtypes = $this->_get_gtype();//获取商品分类
        $this->gtypes = $gtypes;
        
        $this->_my_gardeinfo();
        
    }
    
    /*
     * 用户等级
     */
    protected function _my_gardeinfo(){
        $where_grade = array(
            "grade_id" => $this->memberinfo['grade_id']
        );
        $grade_info = $this->gradeModel->getGrade($where_grade);
        $this->grade_info = $grade_info;
        $this->assign('grade_info',$grade_info);
        return $grade_info;
    }
    

    /*
     * 获取商品分类
     */
    public function _get_gtype(){
        $data = $this->goodstypeModel->getGoodstypeList();
        $gtypes = array();
        foreach($data as $k=>$v){
            $gtypes[$v['gtype_id']] = $v;
        }
        $this->assign('gtype',$gtypes);
        return $gtypes;
    }
    
    
    /*
     * 商品统计
     */
    public function countGoods(){
        //$where = "ischeck=1 AND stock>0";
        $where = "ischeck=1 AND stock>0";
        $count[0] = $this->goodsModel->getGoodsCount($where);//全部商品
        $count[1] = $this->goodsModel->getGoodsCount($where." AND is_new=1");//新品
        $count[2] = $this->goodsModel->getGoodsCount($where." AND is_hot=1");//热卖
        return $count;
    }

    
    /*
     * 商品详情
     */
    public function index(){
        
        $this->assign("nocart",true);//不需要购物车悬浮窗
        
        $goods_id = get("goods_id");
        $where  = $this->_get_goods_where();
        $goods = $this->goodsModel->getGoods($where);
        if(empty($goods)){
            $this->error2("已售空","/mobile/goods/lists");exit;
        }
        $goods['goods_desc'] = stripslashes($goods['goods_desc']);
        //等级价格
        $grade_id = $this->memberinfo['grade_id'];
        $grade_id = $grade_id ? $grade_id : 1;
        
       
        
        $share_data = array(
            "title" => $goods['goods_name']."-".$this->memberinfo['nickname']."分享",
            "description" => $goods['goods_name'],
            "img" => C("UPYUN_DOMAIN").$goods['goods_pic'],
            "params" => "goods_id=".$goods_id,
        );
        $this->setShare($share_data);
        
        $seo_data = array(
            "title" => $goods['goods_name']."-".C("SITE_NAME"),
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
        
        //判断是否已经点赞
        $gvoteModel = new gvoteModel();
        
        $where_vote = array(
            "uid" => $this->uid,
            "goods_id" => $goods_id
        );
        $dianzan = $gvoteModel->getGvote($where_vote);
        if($dianzan){
            //已经点赞
            $this->assign("vtype",0);
        }else{
            //未点赞
            $this->assign("vtype",1);
        }
        
        //商品规格库存价格等
        $where_guige = array(
            "goods_id" => $goods_id,
            "guige_stock" => array("gt",0),
            "guige_price" => array("egt",0),
        );
        $guige = $this->goodsguigeModel->getGoodsguigeLists($where_guige);
        $new_guige = array();
        if(!empty($guige)){
            foreach($guige as $k=>$v){
                $tv = $v['size_id']."_".$v['color_id'];
                $new_guige[$tv] = $v;
                
            }
        }
        $goods['guige'] = $new_guige;

        //规格名称
        $guige_name = $this->goodsguigeModel->getGuige($where_guige);
        $goods['guige_name'] = $guige_name;
        
        $guige_new = unserialize($goods['guige_new']);
        $this->assign('guige_new',$guige_new);
        $guige_names = unserialize($goods['guige_names']);
        $this->assign('guige_names',$guige_names);

	    $goods['shop_price'] = $goods['grade'.$grade_id.'_price'];
        
        
        //积分商城商品
        if($goods['grade_id']>=3) {
            $goods['is_cart']=0;
            $goods['buy_text']='立即购买';
        }

        if($goods['goods_id']==2) {
            $goods['is_cart']=0;
            $goods['buy_text']='立即购买';
            $goods['buy_url']='/mobile/cart/daili?grade=3';
        }elseif($goods['goods_id']==3) {
            $goods['is_cart']=0;
            $goods['buy_text']='立即购买';
            $goods['buy_url']='/mobile/cart/daili?grade=4';
        }
        
        
        $this->assign("goods",$goods);
        $this->assign("grade",$this->memberinfo['grade_id']);
        //判断当前商品是否可以使用购物车
        if(!$goods['is_cart']){
            //不可以使用购物车
            $this->display();
        }else{
            //可以使用购物车
            $this->display('carbuy');
        }
        
        
    }
    
    
    protected function _get_goods_where(){
        $arrow = array(
            "goods_name","goods_id","shop_price","gtype_id"
        );//允许提交的数据
        $data = param_filter($_GET,$arrow,$must=NULL,$filter=true);//过滤数据
    
        $where = "ischeck=1 AND stock>0 ";
        
        //todo
        if(is_set($data,'goods_name')){
            $where .= " AND goods_name like '%".$data['goods_name']."%'";
        }
        if(is_set($data,'goods_id')){
            $where .= " AND goods_id =".$data['goods_id'];
        }
        if(is_set($data,'gtype_id')){
            $where .= " AND gtype_id =".$data['gtype_id'];
        }
        
        if(is_set($data,'shop_price')){
            $where .= " AND shop_price <=".$data['shop_price'];
        }
        
        return $where;
    }
    
    /*
     * 商品列表
     */
    public function lists(){
        $seo_data = array(
            "title" => $this->lang['goods_lists']."-".C("SITE_NAME"),
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
        
        $page = get("p",1);
        $where = $this->_get_goods_where();
        $where .= " and goods_id>={$this->memberinfo['grade_id']}";
        $orderby = get('orderby');
        if($orderby == "pricedown"){
            $order = "shop_price ASC";
        }else if($orderby == "pv"){
            $order = "all_views ASC";
        }else if($orderby == "sales"){
            $order = "sale_all ASC";
        }else{
            $order = "ord ASC";
        }

        $goods_list = $this->goodsModel->getGoodsList($where, $page,$order);
        $g = array();
        
        //等级价格
        $grade_id = $this->memberinfo['grade_id'];
        $grade_id = $grade_id ? $grade_id : 0;

        foreach($goods_list as $k=>$v){
//            if($grade_id == 3){
//                $v['shop_price'] = $v['ceo_price'];
//            }else if($grade_id > 3){
//                $v['shop_price'] = $v['king_price'];
//            }
            $goods['shop_price'] = $goods['grade'.$grade_id.'_price'];
            $g[$v['gtype_id']][] = $v;
        }
        
        
        $this->assign('datalist',$g);
        $this->assign("grade",$this->memberinfo['grade_id']);
        $this->setShare();
        
        
        $this->display();
    }


    /*
     * 商品列表
     */
    public function daili(){
        $seo_data = array(
            "title" => "我要开店",
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
        
        $page = get("p",1);
        $where = array(
            'goods_id' => array('in',array(2,3))
        );


        $goods_list = $this->goodsModel->getGoodsList($where, $page,$order);
        $g = array();
        
        //等级价格
        $grade_id = $this->memberinfo['grade_id'];
        $grade_id = $grade_id ? $grade_id : 0;

        foreach($goods_list as $k=>$v){
            $goods_list[$k]['shop_price'] = $v['grade'.$grade_id.'_price'];
        }
       
        
        $this->assign('datalist',$goods_list);
        $this->assign("grade",$this->memberinfo['grade_id']);
        $this->setShare();
        
        $this->display();
    }

    
    /*
     * 商品点赞
     */
    public function gvote(){
        if(IS_AJAX){
            $goods_id = get('goods_id');
            $vtype = get('vtype');
            $gvoteModel = new gvoteModel();
    
            $data = array(
                "uid" => $this->uid,
                "goods_id" => $goods_id
            );
    
            //查找有没有
            $where_vote = array(
                "uid" => $this->uid,
                "goods_id" => $goods_id
            );
            $dianzan = $gvoteModel->getGvote($where_vote);
            if($dianzan){
                //已经点赞
                $res = $gvoteModel->where($data)->delete();
                if($res){
                    $this->goodsModel->where(array("goods_id",$goods_id))->setDec('vote_nums');
                    return_json(0,"已取消","-1");
                }
            }else{
                $res = $gvoteModel->addGvote($data);
                if($res){
                    $this->goodsModel->where(array("goods_id",$goods_id))->setInc('vote_nums');
                    return_json(0,"已点赞","1");
                }
            }
    
    
            return_json(1,"操作失败");
    
        }
    
    }
    
    
    /*
     * 商品列表
     */
    public function tablists(){
        $gtype = "商品分类";
        $seo_data = array(
            "title" => $gtype . "-" . C("SITE_NAME"),
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
        $page = get("p", 1);
        $goods_list = $this->goodsModel->getGoodsList(1, $page);
        //等级价格
        $grade_id = $this->memberinfo['grade_id'];
        $grade_id = $grade_id ? $grade_id : 1;

        foreach ($goods_list as $k => $v) {
            $v['shop_price'] = $v['grade' . $grade_id . "_price"]; //等级价格
            $g[$v['gtype_id']][] = $v;
        }
        $this->assign('datalist', $g);
        $this->assign("grade", $this->memberinfo['grade_id']);
        $this->setShare();

        layout('Mui/layout');
        $this->display();
    }
    
    
}